﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;

namespace Qplus.UI
{
    public partial class SmileyControl : UserControl
    {
        int smileyPicIndex;
        public SmileyControl()
        {
            InitializeComponent();
            this.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.UserControl_MouseLeftButtonDown), true);
            this.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.UserControl_MouseLeftButtonUp), true);
            //smileyBtn.ClickMode = ClickMode.Press;
        }
        bool editable = false;
        public bool Editable
        {
            set { editable = value; }
            get { return editable; }
        }
        public int Index
        {
            set
            {
                smileyPicIndex = value;
                smileImg.Source = new BitmapImage(new Uri("/Qplus;component/Pic/smiley_" + smileyPicIndex.ToString() + ".png",
                    UriKind.RelativeOrAbsolute));
            }
            get { return smileyPicIndex; }
        }
//event
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutRoot.Background = new SolidColorBrush(new Color() { A = 0xEB, R = 0x00, G = 0xE3, B = 0xFF });
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            LayoutRoot.Background = new SolidColorBrush(Colors.Transparent);
        }
        private void UserControl_GotFocus(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Background = new SolidColorBrush(Colors.Red);
        }
        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            //LayoutRoot.Background = new SolidColorBrush(new Color() { A = 0x00, R = 0x00, G = 0xE3, B = 0xFF });
        }
        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            Key tmpKey = e.Key;
            switch (tmpKey)
            {
                case Key.Back:
                    {
                        if (this.Editable)
                        {
                            this.DelSelf();
                        }
                        break;
                    }
                case Key.Left:
                    {
                        var parent = this.Parent as WrapPanel;
                        var pre = parent.Children.ElementAt(parent.Children.IndexOf(this) - 1);
                        try
                        {
                            (pre as TextBox).Focus();
                            (pre as SmileyControl).Focus();
                        }
                        catch (Exception etry)
                        {
                            Console.WriteLine("{0} Exception caught.", etry);
                        }
                        break;
                    }
                case Key.Right:
                    {
                        var parent = this.Parent as WrapPanel;
                        var next = parent.Children.ElementAt(parent.Children.IndexOf(this) + 1);
                        try
                        {
                            (next as TextBox).Focus();
                            (next as TextBox).SelectionStart = 0;
                            (next as SmileyControl).Focus();
                        }
                        catch (Exception etry)
                        {
                            Console.WriteLine("{0} Exception caught.", etry);
                        }
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            //RecordControl tmpRC = new RecordControl() { Smiley = this };
            //(App.Current.RootVisual as ChatFriend).recordWP.Children.Add(tmpRC);
        }

        private void smileyBtn_Click(object sender, RoutedEventArgs e)
        {
            if (Editable)
            {
                this.Focus();
            }
            else
            {
                SmileyControl tmpSC = new SmileyControl() { Editable = true, Index = this.Index };
                ((Application.Current as App).RootFrame.Content as ChatFriend).inputRTC.Smiley = tmpSC;
                TextBox newTxt = new TextBox() { Style = (Style)App.Current.Resources["noBorderTB"] };
                newTxt.KeyUp += new KeyEventHandler(newTxt_KeyUp);
                ((Application.Current as App).RootFrame.Content as ChatFriend).inputRTC.richTxtCtrlWP.Children.Add(newTxt);
            }
            //var isInRTC =          (((this.Parent as WrapPanel).Parent as ScrollViewer).Parent as Grid).Parent as RichTextControl;
            //var tmp = (((this.Parent as WrapPanel).Parent as ScrollViewer).Parent as Grid).Parent as Grid;
            //if (tmp != null)
            //{
            //    var isInSmileyCtrl = (tmp.Parent as Grid).Parent as PopupContainer;
            //    if (isInSmileyCtrl != null)
            //    {
            //        SmileyControl tmpSC = new SmileyControl() { Index = this.Index };
            //        ((Application.Current as App).RootFrame.Content as ChatFriend).inputRTC.Smiley = tmpSC;
            //        TextBox newTxt = new TextBox() { Style = (Style)App.Current.Resources["noBorderTB"] };
            //        ((Application.Current as App).RootFrame.Content as ChatFriend).inputRTC.richTxtCtrlWP.Children.Add(newTxt);
            //    }
            //}
            ////var isInRecord = ((((((((this.Parent as WrapPanel).Parent as ScrollViewer).Parent as Grid).Parent as RichTextControl).Parent as WrapPanel).Parent as ScrollViewer).Parent as Grid).Parent as Grid).Parent as ChatFriend;
            
            //if (isInRTC != null)
            //{
            //    if ((isInRTC as RichTextControl).IsInput == true)
            //    {
            //        this.Focus();
            //    }
            //    else
            //    { }
            //}
        }

        void newTxt_KeyUp(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
            TextBox tmpTxt = sender as TextBox;
            Key tmpKey = e.Key;
            switch (tmpKey)
            {
                case Key.Back:
                    {
                        if (tmpTxt.Text == "")
                        {
                            try
                            {
                                var parent = this.Parent as WrapPanel;
                                int index = parent.Children.IndexOf(tmpTxt);
                                var pre = parent.Children.ElementAt(index - 1);
                                var next = parent.Children.ElementAt(index);
                                (pre as SmileyControl).DelSelf();
                            }
                            catch (Exception etry)
                            {
                                Console.WriteLine("{0} Exception caught.", etry);
                            }
                        }
                        break;
                    }
                case Key.Left:
                    {
                        if (tmpTxt.SelectionStart == 0)
                        {
                            try
                            {
                                var parent = this.Parent as WrapPanel;
                                int index = parent.Children.IndexOf(tmpTxt);
                                var pre = parent.Children.ElementAt(index - 1);
                                (pre as SmileyControl).Focus();
                            }
                            catch (Exception etry)
                            {
                                Console.WriteLine("{0} Exception caught.", etry);
                            }
                        }
                        break;
                    }
                case Key.Right:
                    {
                        if (tmpTxt.SelectionStart == tmpTxt.Text.Length - 1)
                        {
                            try
                            {
                                var parent = this.Parent as WrapPanel;
                                var next = parent.Children.ElementAt(parent.Children.IndexOf(tmpTxt) + 1);
                                (next as SmileyControl).Focus();
                            }
                            catch (Exception etry)
                            {
                                Console.WriteLine("{0} Exception caught.", etry);
                            }
                        }
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
        private void smileyBtn_LostMouseCapture(object sender, MouseEventArgs e)
        {
            LayoutRoot.Background = new SolidColorBrush(Colors.Transparent);
        }
//del self
        public void DelSelf()
        {
            try
            {
                var parent = this.Parent as WrapPanel;
                var pre = parent.Children.ElementAt(parent.Children.IndexOf(this) - 1);
                var next = parent.Children.ElementAt(parent.Children.IndexOf(this) + 1);
                parent.Children.Remove(this);
                if ((pre as TextBox) != null && (next as TextBox) != null)
                {
                    (pre as TextBox).Text += (next as TextBox).Text;
                    parent.Children.Remove(next as TextBox);
                }
                (pre as TextBox).Focus();
                (pre as SmileyControl).Focus();
            }
            catch (Exception etry)
            {
                Console.WriteLine("{0} Exception caught.", etry);
            }
        }
    }
}
